package com.youku.plugins.interaction.model.data
{
   import flash.events.EventDispatcher;
   import flash.display.Loader;
   import flash.utils.Timer;
   import flash.events.Event;
   import flash.events.IOErrorEvent;
   import flash.events.SecurityErrorEvent;
   import flash.events.TimerEvent;
   import flash.net.URLRequest;
   import flash.system.LoaderContext;
   import com.youku.plugins.interaction.events.InteractionEvent;
   import flash.display.LoaderInfo;
   
   public class InteractionList extends EventDispatcher
   {
      
      public function InteractionList(param1:PrivateClass) {
         super();
         this.init();
      }
      
      private static var _instance:InteractionList;
      
      public static function getInstance() : InteractionList {
         if(_instance == null)
         {
            _instance = new InteractionList(new PrivateClass());
         }
         return _instance as InteractionList;
      }
      
      private var _itemArr:Array;
      
      private var _loader:Loader;
      
      private var _loaderTimer:Timer;
      
      private var _timeout:Number = 10000.0;
      
      private function init() : void {
         this._itemArr = [];
         this._loader = new Loader();
         this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE,this.onLoadComplete);
         this._loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,this.onLoadError);
         this._loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onLoadError);
         this._loaderTimer = new Timer(this._timeout);
         this._loaderTimer.addEventListener(TimerEvent.TIMER,this.onLoadTimeout);
      }
      
      public function addToList(param1:String, param2:String) : void {
         this._itemArr.push(
            {
               "url":param1,
               "type":param2
            });
      }
      
      public function load() : void {
         if(this._itemArr == null || this._itemArr.length == 0)
         {
            return;
         }
         if(this._loaderTimer.running)
         {
            return;
         }
         this._loader.load(new URLRequest(this._itemArr[0].url),new LoaderContext(true));
         this._loaderTimer.reset();
         this._loaderTimer.start();
      }
      
      private function onLoadComplete(param1:Event) : void {
         if(this._itemArr.length == 0)
         {
            return;
         }
         this._loaderTimer.stop();
         dispatchEvent(new InteractionEvent(InteractionEvent.SWF_LOAD_COMPLETE,
            {
               "item":(param1.target as LoaderInfo).loader.content,
               "type":this._itemArr[0].type
            }));
         this._itemArr.shift();
         this.load();
      }
      
      private function onLoadError(param1:Event) : void {
         if(this._itemArr.length == 0)
         {
            return;
         }
         this._loaderTimer.stop();
         dispatchEvent(new InteractionEvent(InteractionEvent.SWF_LOAD_ERROR,
            {
               "type":this._itemArr[0].type,
               "errorCode":400
            }));
         this._itemArr.shift();
         this.load();
      }
      
      private function onLoadTimeout(param1:TimerEvent) : void {
         if(this._itemArr.length == 0)
         {
            return;
         }
         this._loaderTimer.stop();
         dispatchEvent(new InteractionEvent(InteractionEvent.SWF_LOAD_ERROR,
            {
               "type":this._itemArr[0].type,
               "errorCode":800
            }));
         this._itemArr.shift();
         this.load();
      }
      
      public function dispose() : void {
         try
         {
            this._loader.unload();
         }
         catch(e:Error)
         {
         }
      }
   }
}
class PrivateClass extends Object
{
   
   function PrivateClass() {
      super();
   }
}
